package com.wise.controller;

import com.wise.dao.BlogDao;
import com.wise.dao.SortDao;
import com.wise.dao.TagDao;
import com.wise.pojo.Blog;
import com.wise.pojo.Sort;
import com.wise.pojo.Tag;
import com.wise.pojo.User;
import com.wise.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BlogLoadController {

    @Autowired
    BlogDao blogDao;
    @Autowired
    TagDao tagDao;
    @Autowired
    SortDao sortDao;

    @CrossOrigin
    @PostMapping(value = "api/loadsidebar")
    @ResponseBody
    public Result loadsidebar(@RequestBody User requestUser){
        int uid=requestUser.getId();
        int blogcount = blogDao.findBlogCountByUid(uid);
        int tagcount = tagDao.findTagCountByUid(uid);
        int sortcount = sortDao.findSortCountByUid(uid);
        List<Tag> tags = tagDao.findTagByUid(uid);
        List<Sort> sorts = sortDao.findSortByUid(uid);
        List<Blog> blogs = blogDao.findBlogByUid(uid);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("blogcount",blogcount);
        resultMap.put("tagcount",tagcount);
        resultMap.put("sortcount",sortcount);
        resultMap.put("tags",tags);
        resultMap.put("sorts",sorts);
        resultMap.put("blogs",blogs);
        if(null==resultMap){
            return new Result(400,"加载失败",null);
        }
        return new Result(200,"加载成功",resultMap);
    }
}
